package cn.jiedh.app.meiweidk.Utils.UI;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.interfaces.onLoadFinishListener;
import cn.jiedh.app.meiweidk.Utils.tool.LoadUtils;


/**
 * 创建人 PACY
 * 创建时间 18/11/21 14:56.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述 绿色通道的dialog
 */

public class GreenDialog extends Dialog {
    private Context context;

    private ImageView title_img;
    private ImageView loading;

    private TextView loading_lable;
    private TextView load_text;

    private int stage = 1;

    onLoadFinishListener finishListener;

    public GreenDialog(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    /**
     * 初始化view
     */
    private void init() {
        View view = LayoutInflater.from(context).inflate(R.layout.green_load_dialog, null);

        title_img = view.findViewById(R.id.title_img);
        loading = view.findViewById(R.id.loading);
        loading_lable = view.findViewById(R.id.loading_lable);
        load_text = view.findViewById(R.id.load_text);

        LoadUtils.loadGIF(context, R.drawable.green_loading, loading);
        setCanceledOnTouchOutside(false);
//        setCancelable(false);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setContentView(view);
    }


    /**
     * 开始
     */
    public void startStage(){
        stage=1;
        stage1();
    }

    /**
     * 基本信息核实阶段
     */
    private void stage1() {
        LoadUtils.loadGIF(context, R.drawable.jbxx, title_img);
        loading_lable.setText("正在进行基本信息核实\n请勿退出");
        timer.start();
    }

    /**
     * 信用评估阶段
     */
    private void stage2() {
        LoadUtils.loadGIF(context, R.drawable.xypg, title_img);
        loading_lable.setText("正在进行信用评估\n请勿退出");
        timer.start();
    }

    /**
     * 整体风险评测阶段
     */
    private void stage3() {
        LoadUtils.loadGIF(context, R.drawable.fxpc, title_img);
        loading_lable.setText("正在进行整体风险评测\n请勿退出");
        timer.start();
    }

    /**
     * 获取额度阶段
     */
    private void stage4() {
        LoadUtils.loadGIF(context, R.drawable.edu, title_img);
        loading_lable.setText("获取额度详情\n请勿退出");
        timer.start();
    }

    /**
     * 模拟进度
     */
    private CountDownTimer timer = new CountDownTimer(2 * 1000, 20) {
        @Override
        public void onTick(long millisUntilFinished) {
            load_text.setText((100 - (millisUntilFinished / 20)) + "%");
        }

        @Override
        public void onFinish() {
            stage++;
            load_text.setText("100%");
            switch (stage) {
                case 2:
                    stage2();
                    break;
                case 3:
                    stage3();
                    break;
                case 4:
                    stage4();
                    break;
                case 5://结束
                    if(null!=finishListener){
                        finishListener.onFinish();
                    }else{
                        throw new RuntimeException("need finishListener");
                    }
                    break;
            }
        }
    };


    public void setFinishListener(onLoadFinishListener finishListener){
        this.finishListener=finishListener;
    }
}
